const int pad0=2;   // Specified input PIN for number pads
const int pad1=3;
const int pad2=4;
const int pad3=5;
const int pad4=6;
const int pad5=7;
const int pad6=8;
const int pad7=9;
const int pad8=10;
const int pad9=11;

int ledRow1=35;    // Specified output PIN for LED number rows
int ledRow2=33;
int ledRow3=31;

int trial = 0;    // Number of Trials
int count = 0;    // Input Counts(Number entered)
int T = 0;        // True = Count for correct answers

void setup(){
  
  Serial.begin(9600);
  
  pinMode(pad0,INPUT);  // Initialized INPUT ports
  pinMode(pad1,INPUT);
  pinMode(pad2,INPUT);
  pinMode(pad3,INPUT);
  pinMode(pad4,INPUT);
  pinMode(pad5,INPUT);
  pinMode(pad6,INPUT);
  pinMode(pad7,INPUT);
  pinMode(pad8,INPUT);
  pinMode(pad9,INPUT);

  pinMode(ledRow1,OUTPUT);  // Initialized OUTPUT ports
  pinMode(ledRow2,OUTPUT);
  pinMode(ledRow3,OUTPUT);
  
}

void loop(){

  digitalWrite(ledRow1, LOW);    //  Initial LED Settings
  digitalWrite(ledRow2, LOW);    //  Every row turned off
  digitalWrite(ledRow3, LOW);
  
  // ******************************************************************
  // *********************** THE FIRST ROW ****************************
  // ******************************************************************

  if(trial == 0) {    // Trial = 0 is the first row 
     
     digitalWrite(ledRow1, HIGH); // Turn on LED row 1
     
     if(count == 0) {                 // First input number
        if(digitalRead(pad0)==HIGH){  // HIGH = button pressed
          count++;                    // count increase according to number of inputs
          Serial.println("Input: 0"); // Print the entered number
          delay(1000);                // Delay for 1 sec.
          }
        else if(digitalRead(pad1)==HIGH){
          count++;
          Serial.println("Input: 1");
          delay(1000);
          }
        else if(digitalRead(pad2)==HIGH){
          count++;
          Serial.println("Input: 2");
          delay(1000);
          }
        else if(digitalRead(pad3)==HIGH){
          count++;
          Serial.println("Input: 3");
          delay(1000);
          }
        else if(digitalRead(pad4)==HIGH){
          count++;
          Serial.println("Input: 4");
          delay(1000);
          }
        else if(digitalRead(pad5)==HIGH){
          count++;
          Serial.println("Input: 5");
          delay(1000);
          }
        else if(digitalRead(pad6)==HIGH){
          count++;
          Serial.println("Input: 6");
          delay(1000);
          }
        else if(digitalRead(pad7)==HIGH){
          count++;
          Serial.println("Input: 7");
          delay(1000);
          }
        else if(digitalRead(pad8)==HIGH){
          count++;
          Serial.println("Input: 8");
          delay(1000);
          }
        else if(digitalRead(pad9)==HIGH){
          count++;
            T++;                           // Correct answer, true = true+1
          Serial.println("Input: 9");
          delay(1000);
        }
      }
      else if(count == 1) {              // Second input number
        if(digitalRead(pad0)==HIGH){
          count++;
          Serial.println("Input: 0");
          delay(1000);
          }
        else if(digitalRead(pad1)==HIGH){
          count++;
          Serial.println("Input: 1");
          delay(1000);
          }
        else if(digitalRead(pad2)==HIGH){
          count++;
          Serial.println("Input: 2");
          delay(1000);
          }
        else if(digitalRead(pad3)==HIGH){
          count++;
          Serial.println("Input: 3");
          delay(1000);
          }
        else if(digitalRead(pad4)==HIGH){
          count++;
          Serial.println("Input: 4");
          delay(1000);
          }
        else if(digitalRead(pad5)==HIGH){
          count++;
          T++;
          Serial.println("Input: 5");
          delay(1000);
          }
        else if(digitalRead(pad6)==HIGH){
          count++;
          Serial.println("Input: 6");
          delay(1000);
          }
        else if(digitalRead(pad7)==HIGH){
          count++;
          Serial.println("Input: 7");
          delay(1000);
          }
        else if(digitalRead(pad8)==HIGH){
          count++;
          Serial.println("Input: 8");
          delay(1000);
          }
        else if(digitalRead(pad9)==HIGH){
          count++;
          Serial.println("Input: 9");
          delay(1000);
        }
      }
   
   else if(count == 2) {    // Check if the entered numbers corrected
     if(T==2) {     // If the answer is correct
       digitalWrite(ledRow1, LOW);  // Turn off LED
       Serial.println("Your answers are CORRECT !");
       Serial.println("Continue to the Second Row...");
       count = 0;   // Reset count
       trial = 1;   // Trial = 1, go to the next LED row
       T = 0;       // Reset true state
       delay(1000);
     }
     else {       // If the answer is not correct
       digitalWrite(ledRow1, LOW);   // Turn off LED
       Serial.println("Your answers are INCORRECT !!");
       Serial.println("Please try again...");
       count = 0;    // Reset count
       trial = 0;    // Reset trial, repeat the same LED row again
       T = 0;        // Reset true state
       delay(1000);
     }
   }  
 }


 
  // ******************************************************************
  // ********************** THE SECOND ROW ****************************
  // ******************************************************************

   if(trial == 1) {
     
     digitalWrite(ledRow2, HIGH);
     
     if(count == 0) {                 // 1st input number
        if(digitalRead(pad0)==HIGH){
          count++;
          Serial.println("Input: 0");
          delay(1000);
          }
        else if(digitalRead(pad1)==HIGH){
          count++;
          Serial.println("Input: 1");
          delay(1000);
          }
        else if(digitalRead(pad2)==HIGH){
          count++;
          Serial.println("Input: 2");
          delay(1000);
          }
        else if(digitalRead(pad3)==HIGH){
          count++;
          Serial.println("Input: 3");
          delay(1000);
          }
        else if(digitalRead(pad4)==HIGH){
          count++;
          Serial.println("Input: 4");
          delay(1000);
          }
        else if(digitalRead(pad5)==HIGH){
          count++;
          Serial.println("Input: 5");
          delay(1000);
          }
        else if(digitalRead(pad6)==HIGH){
          count++;
          Serial.println("Input: 6");
          delay(1000);
          }
        else if(digitalRead(pad7)==HIGH){
          count++;
          Serial.println("Input: 7");
          delay(1000);
          }
        else if(digitalRead(pad8)==HIGH){
          count++;
          T++;
          Serial.println("Input: 8");
          delay(1000);
          }
        else if(digitalRead(pad9)==HIGH){
          count++;
          Serial.println("Input: 9");
          delay(1000);
        }
     }
   
   else if(count == 1) {              // 2nd input number
      if(digitalRead(pad0)==HIGH){
        count++;
        Serial.println("Input: 0");
        delay(1000);
        }
      else if(digitalRead(pad1)==HIGH){
        count++;
        Serial.println("Input: 1");
        delay(1000);
        }
      else if(digitalRead(pad2)==HIGH){
        count++;
        Serial.println("Input: 2");
        delay(1000);
        }
      else if(digitalRead(pad3)==HIGH){
        count++;
        T++;
        Serial.println("Input: 3");
        delay(1000);
        }
      else if(digitalRead(pad4)==HIGH){
        count++;
        Serial.println("Input: 4");
        delay(1000);
        }
      else if(digitalRead(pad5)==HIGH){
        count++;
        Serial.println("Input: 5");
        delay(1000);
        }
      else if(digitalRead(pad6)==HIGH){
        count++;
        Serial.println("Input: 6");
        delay(1000);
        }
      else if(digitalRead(pad7)==HIGH){
        count++;
        Serial.println("Input: 7");
        delay(1000);
        }
      else if(digitalRead(pad8)==HIGH){
        count++;
        Serial.println("Input: 8");
        delay(1000);
        }
      else if(digitalRead(pad9)==HIGH){
        count++;
        Serial.println("Input: 9");
        delay(1000);
      }
   }
   
   else if(count == 2) {              // 3rd input number
      if(digitalRead(pad0)==HIGH){
        count++;
        Serial.println("Input: 0");
        delay(1000);
        }
      else if(digitalRead(pad1)==HIGH){
        count++;
        Serial.println("Input: 1");
        delay(1000);
        }
      else if(digitalRead(pad2)==HIGH){
        count++;
        Serial.println("Input: 2");
        delay(1000);
        }
      else if(digitalRead(pad3)==HIGH){
        count++;
        Serial.println("Input: 3");
        delay(1000);
        }
      else if(digitalRead(pad4)==HIGH){
        count++;
        T++;
        Serial.println("Input: 4");
        delay(1000);
        }
      else if(digitalRead(pad5)==HIGH){
        count++;
        Serial.println("Input: 5");
        delay(1000);
        }
      else if(digitalRead(pad6)==HIGH){
        count++;
        Serial.println("Input: 6");
        delay(1000);
        }
      else if(digitalRead(pad7)==HIGH){
        count++;
        Serial.println("Input: 7");
        delay(1000);
        }
      else if(digitalRead(pad8)==HIGH){
        count++;
        Serial.println("Input: 8");
        delay(1000);
        }
      else if(digitalRead(pad9)==HIGH){
        count++;
        Serial.println("Input: 9");
        delay(1000);
      }
   }
   
   else if(count == 3) {              // 4th input number
      if(digitalRead(pad0)==HIGH){
        count++;
        Serial.println("Input: 0");
        delay(1000);
        }
      else if(digitalRead(pad1)==HIGH){
        count++;
        Serial.println("Input: 1");
        delay(1000);
        }
      else if(digitalRead(pad2)==HIGH){
        count++;
        Serial.println("Input: 2");
        delay(1000);
        }
      else if(digitalRead(pad3)==HIGH){
        count++;
        Serial.println("Input: 3");
        delay(1000);
        }
      else if(digitalRead(pad4)==HIGH){
        count++;
        Serial.println("Input: 4");
        delay(1000);
        }
      else if(digitalRead(pad5)==HIGH){
        count++;
        Serial.println("Input: 5");
        delay(1000);
        }
      else if(digitalRead(pad6)==HIGH){
        count++;
        Serial.println("Input: 6");
        delay(1000);
        }
      else if(digitalRead(pad7)==HIGH){
        count++;
        T++;
        Serial.println("Input: 7");
        delay(1000);
        }
      else if(digitalRead(pad8)==HIGH){
        count++;
        Serial.println("Input: 8");
        delay(1000);
        }
      else if(digitalRead(pad9)==HIGH){
        count++;
        Serial.println("Input: 9");
        delay(1000);
      }
   }
   
   else if(count == 4) {    // Check if the entered numbers corrected
     if(T==4) {
       digitalWrite(ledRow2, LOW);
       Serial.println("Your answers are CORRECT !");
       Serial.println("Continue to the Third Row...");
       count = 0;
       trial = 2;
       T = 0;
       delay(1000);
     }
     else {
       digitalWrite(ledRow2, LOW);
       Serial.println("Your answers are INCORRECT !!");
       Serial.println("Please try again...");
       count = 0;
       trial = 0;
       T = 0;
       delay(1000);
     }
   }
 }

// ******************************************************************
  // ********************** THE THIRD ROW ****************************
  // ******************************************************************

   if(trial == 2) {
     
     digitalWrite(ledRow3, HIGH);
     
     if(count == 0) {                 // 1st input number
        if(digitalRead(pad0)==HIGH){
          count++;
          Serial.println("Input: 0");
          delay(1000);
          }
        else if(digitalRead(pad1)==HIGH){
          count++;
          Serial.println("Input: 1");
          delay(1000);
          }
        else if(digitalRead(pad2)==HIGH){
          count++;
          Serial.println("Input: 2");
          delay(1000);
          }
        else if(digitalRead(pad3)==HIGH){
          count++;
          T++;
          Serial.println("Input: 3");
          delay(1000);
          }
        else if(digitalRead(pad4)==HIGH){
          count++;
          Serial.println("Input: 4");
          delay(1000);
          }
        else if(digitalRead(pad5)==HIGH){
          count++;
          Serial.println("Input: 5");
          delay(1000);
          }
        else if(digitalRead(pad6)==HIGH){
          count++;
          Serial.println("Input: 6");
          delay(1000);
          }
        else if(digitalRead(pad7)==HIGH){
          count++;
          Serial.println("Input: 7");
          delay(1000);
          }
        else if(digitalRead(pad8)==HIGH){
          count++;
          Serial.println("Input: 8");
          delay(1000);
          }
        else if(digitalRead(pad9)==HIGH){
          count++;
          Serial.println("Input: 9");
          delay(1000);
        }
     }
   
   else if(count == 1) {              // 2nd input number
      if(digitalRead(pad0)==HIGH){
        count++;
        Serial.println("Input: 0");
        delay(1000);
        }
      else if(digitalRead(pad1)==HIGH){
        count++;
        Serial.println("Input: 1");
        delay(1000);
        }
      else if(digitalRead(pad2)==HIGH){
        count++;
        Serial.println("Input: 2");
        delay(1000);
        }
      else if(digitalRead(pad3)==HIGH){
        count++;
        Serial.println("Input: 3");
        delay(1000);
        }
      else if(digitalRead(pad4)==HIGH){
        count++;
        Serial.println("Input: 4");
        delay(1000);
        }
      else if(digitalRead(pad5)==HIGH){
        count++;
        Serial.println("Input: 5");
        delay(1000);
        }
      else if(digitalRead(pad6)==HIGH){
        count++;
        Serial.println("Input: 6");
        delay(1000);
        }
      else if(digitalRead(pad7)==HIGH){
        count++;
        Serial.println("Input: 7");
        delay(1000);
        }
      else if(digitalRead(pad8)==HIGH){
        count++;
        T++;
        Serial.println("Input: 8");
        delay(1000);
        }
      else if(digitalRead(pad9)==HIGH){
        count++;
        Serial.println("Input: 9");
        delay(1000);
      }
   }
   
   else if(count == 2) {              // 3rd input number
      if(digitalRead(pad0)==HIGH){
        count++;
        Serial.println("Input: 0");
        delay(1000);
        }
      else if(digitalRead(pad1)==HIGH){
        count++;
        Serial.println("Input: 1");
        delay(1000);
        }
      else if(digitalRead(pad2)==HIGH){
        count++;
        Serial.println("Input: 2");
        delay(1000);
        }
      else if(digitalRead(pad3)==HIGH){
        count++;
        Serial.println("Input: 3");
        delay(1000);
        }
      else if(digitalRead(pad4)==HIGH){
        count++;
        Serial.println("Input: 4");
        delay(1000);
        }
      else if(digitalRead(pad5)==HIGH){
        count++;
        Serial.println("Input: 5");
        delay(1000);
        }
      else if(digitalRead(pad6)==HIGH){
        count++;
        Serial.println("Input: 6");
        delay(1000);
        }
      else if(digitalRead(pad7)==HIGH){
        count++;
        T++;
        Serial.println("Input: 7");
        delay(1000);
        }
      else if(digitalRead(pad8)==HIGH){
        count++;
        Serial.println("Input: 8");
        delay(1000);
        }
      else if(digitalRead(pad9)==HIGH){
        count++;
        Serial.println("Input: 9");
        delay(1000);
      }
   }
   
   else if(count == 3) {              // 4th input number
      if(digitalRead(pad0)==HIGH){
        count++;
        Serial.println("Input: 0");
        delay(1000);
        }
      else if(digitalRead(pad1)==HIGH){
        count++;
        Serial.println("Input: 1");
        delay(1000);
        }
      else if(digitalRead(pad2)==HIGH){
        count++;
        Serial.println("Input: 2");
        delay(1000);
        }
      else if(digitalRead(pad3)==HIGH){
        count++;
        Serial.println("Input: 3");
        delay(1000);
        }
      else if(digitalRead(pad4)==HIGH){
        count++;
        Serial.println("Input: 4");
        delay(1000);
        }
      else if(digitalRead(pad5)==HIGH){
        count++;
        Serial.println("Input: 5");
        delay(1000);
        }
      else if(digitalRead(pad6)==HIGH){
        count++;
        Serial.println("Input: 6");
        delay(1000);
        }
      else if(digitalRead(pad7)==HIGH){
        count++;
        Serial.println("Input: 7");
        delay(1000);
        }
      else if(digitalRead(pad8)==HIGH){
        count++;
        Serial.println("Input: 8");
        delay(1000);
        }
      else if(digitalRead(pad9)==HIGH){
        count++;
        T++;
        Serial.println("Input: 9");
        delay(1000);
      }
   }
   
    else if(count == 4) {              // 5th input number
      if(digitalRead(pad0)==HIGH){
        count++;
        Serial.println("Input: 0");
        delay(1000);
        }
      else if(digitalRead(pad1)==HIGH){
        count++;
        Serial.println("Input: 1");
        delay(1000);
        }
      else if(digitalRead(pad2)==HIGH){
        count++;
        T++;
        Serial.println("Input: 2");
        delay(1000);
        }
      else if(digitalRead(pad3)==HIGH){
        count++;
        Serial.println("Input: 3");
        delay(1000);
        }
      else if(digitalRead(pad4)==HIGH){
        count++;
        Serial.println("Input: 4");
        delay(1000);
        }
      else if(digitalRead(pad5)==HIGH){
        count++;
        Serial.println("Input: 5");
        delay(1000);
        }
      else if(digitalRead(pad6)==HIGH){
        count++;
        Serial.println("Input: 6");
        delay(1000);
        }
      else if(digitalRead(pad7)==HIGH){
        count++;
        Serial.println("Input: 7");
        delay(1000);
        }
      else if(digitalRead(pad8)==HIGH){
        count++;
        Serial.println("Input: 8");
        delay(1000);
        }
      else if(digitalRead(pad9)==HIGH){
        count++;
        Serial.println("Input: 9");
        delay(1000);
      }
   }
   
    else if(count == 5) {              // 6th input number
      if(digitalRead(pad0)==HIGH){
        count++;
        Serial.println("Input: 0");
        delay(1000);
        }
      else if(digitalRead(pad1)==HIGH){
        count++;
        Serial.println("Input: 1");
        delay(1000);
        }
      else if(digitalRead(pad2)==HIGH){
        count++;
        Serial.println("Input: 2");
        delay(1000);
        }
      else if(digitalRead(pad3)==HIGH){
        count++;
        Serial.println("Input: 3");
        delay(1000);
        }
      else if(digitalRead(pad4)==HIGH){
        count++;
        T++;
        Serial.println("Input: 4");
        delay(1000);
        }
      else if(digitalRead(pad5)==HIGH){
        count++;
        Serial.println("Input: 5");
        delay(1000);
        }
      else if(digitalRead(pad6)==HIGH){
        count++;
        Serial.println("Input: 6");
        delay(1000);
        }
      else if(digitalRead(pad7)==HIGH){
        count++;
        Serial.println("Input: 7");
        delay(1000);
        }
      else if(digitalRead(pad8)==HIGH){
        count++;
        Serial.println("Input: 8");
        delay(1000);
        }
      else if(digitalRead(pad9)==HIGH){
        count++;
        Serial.println("Input: 9");
        delay(1000);
      }
   }
   
   else if(count == 6) {    // Check if the entered numbers corrected
     if(T==6) {
       digitalWrite(ledRow3, LOW);
       Serial.println("Your answers are CORRECT !");
       Serial.println("Please Contact the Officer");
       count = 0;
       trial = 3;
       T = 0;
       delay(1000);
     }
     else {
       digitalWrite(ledRow3, LOW);
       Serial.println("Your answers are INCORRECT !!");
       Serial.println("Please try again...");
       count = 0;
       trial = 0;
       T = 0;
       delay(1000);
     }
   }   
 }
   
     

    

}